package sk.hackcraft.util.algorithm;

public class Search
{
	public static int findSmallestGreater(Comparable[] array, Comparable searchObject)
	{
		int left = 0;
		int right = array.length - 1;

		while (left <= right)
		{
			// Zabezpečenie funkčnosti aj pri súčte vačšom ako Integer.MAX_VALUE
			int middle = (left + right) >>> 1;
			
			if (searchObject.compareTo(array[middle]) >= 1)
			{
				left = middle+1;
			}
			else if (searchObject.compareTo(array[middle]) <= -1)
			{
				if (middle == 0 || searchObject.compareTo(array[middle - 1]) >= 1)
				{
					return middle;
				}
				else
				{
					right = middle - 1;
				}
			}
			else
			{
				return middle + 1;
			}
		}
		
		return -1;
	}
}
